package org.meb.speedway.web;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main implements Serializable {

	private static final long serialVersionUID = -3015486826912438195L;

	private static class A implements Cloneable, Serializable {
		private int field = 1;

		@Override
		public Object clone() throws CloneNotSupportedException {
			// TODO Auto-generated method stub
			return super.clone();
		}
	}

	private A a;

	/**
	 * @param args
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Main main = new Main();
		main.a = new A();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		new ObjectOutputStream(baos).writeObject(main);
		main.a.field = 2;
		Main copy = (Main) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))
				.readObject();
		System.out.println(main.a == copy.a);
	}

	private void writeObject(ObjectOutputStream oos) throws IOException, CloneNotSupportedException {
		oos.defaultWriteObject();
		oos.writeObject(a.clone());
	}

	private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
		ois.defaultReadObject();
		a = (A) ois.readObject();
	}
}
